package com.zjuee.web.ledger.controller;

import com.zjuee.base.bean.response.R;
import com.zjuee.base.constant.Common;
import com.zjuee.base.dto.common.login.LoginDto;
import com.zjuee.base.valid.Insert;
import com.zjuee.base.valid.Update;
import com.zjuee.service.PlanDangerService;
import com.zjuee.service.PlanService;
import com.zjuee.base.model.common.plan.PlanDanger;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;
import java.util.List;

@Slf4j
@RestController
@RequestMapping(Common.Service.ZJUEE_CO_MAIN_PLAN_URL_PREFIX)
public class PlanDangerController{
    @Resource
    private PlanDangerService planDangerService;
    @Resource
    private PlanService planService;
    @Resource
    private LoginDto loginDto;

    /**
     * 新增危废产生计划信息 PlanDanger
     *
     * @param planDanger PlanDanger
     * @return Long
     */
    @PostMapping("/danger/add")
    public R<Long> addPlanDanger(@Validated(Insert.class) @RequestBody PlanDanger planDanger) {
        if (planDanger.getWpiId() == null)
            return R.fail("计划ID不能为空！");
        if (planDanger.getWpdModel() == null)
            return R.fail("计划类别不能为空！");
        if (planDanger.getWpdType() == null)
            return R.fail("废物类别不能为空！");
        if (planDanger.getWpdBcode() == null)
            return R.fail("危废代码(大类)不能为空！");
        if (planDanger.getWpdScode() == null)
            return R.fail("危废代码(小类)不能为空！");
        if (planDanger.getWpdName() == null)
            return R.fail("危废名称不能为空！");
        if (planDanger.getWpdPreStock() == null)
            return R.fail("上年底库存量不能为空！");
        if (planDanger.getWpdThisProduce() == null)
            return R.fail("本年度计划产生量不能为空！");
        return R.ok(planDangerService.add(planDanger));
    }

    /**
     * 修改危废产生计划信息 PlanDanger
     *
     * @param planDanger PlanDanger
     * @return Boolean
     */
    @PutMapping("/danger/update")
    public R<Boolean> updatePlanDanger(@Validated(Update.class) @RequestBody PlanDanger planDanger) {
        if (planDanger.getWpiId() == null) {
            return R.fail("计划ID为空！");
        }
        return R.ok(planDangerService.update(planDanger));
    }

//     
//    public R<Boolean> deletePlanDanger(@NotNull Long id, @NotNull String wpiYear) {
//        Boolean type = false;
//        if (wpiYear.equals(DateUtils.getYear(new Date()))) {
//            type = planDangerService.delete(id, loginDto.getPlanId());
//        } else {
//            PlanInfo plan = planService.selectInfoByBciId(loginDto.getOrgBusinessId(), wpiYear);
//            if (plan != null) {
//                type = planDangerService.delete(id, plan.getId());
//            } else {
//                return R.fail("删除危废计划失败");
//            }
//        }
//        if (type) {
//            return R.ok("success");
//        }
//        return R.fail("删除危废计划失败");
//    }
    /**
     * 获取危废小代码
     *
     * @return List<String>>
     */
    @GetMapping("/danger/getScode")
    public R<List<String>> getScode(Long wpiId,String wpdType) {
        List<String> wpdCodes = planDangerService.getScodeByWpiId(wpiId,wpdType);
        return R.ok(wpdCodes);
    }

    /**
     * 通过危废小代码获取危废名称
     *
     * @param scode   String
     * @return String
     */
    @GetMapping("/danger/getName/{scode}")
    public R<List<String>> getNameByScode(@NotNull @PathVariable(value = "scode") String scode,Long wpiId,String wpdType) {
        return R.ok(planDangerService.getNameByScode(scode,wpiId,wpdType));
    }

    /**
     * 通过 PlanDanger部分内容 查询计划
     *
     * @param planDanger  PlanDanger
     * @return List<PlanDanger>>
     */
    @PostMapping("/danger/getPlanDanger")
    public R<List<PlanDanger>> getPlanDanger(@RequestBody PlanDanger planDanger) {
        return R.ok(planDangerService.getPlanDanger(planDanger));
    }
}
